#!/bin/bash

set -e

trap 'trap - SIGTERM && kill -- -$$' SIGINT SIGTERM EXIT

MAPD_TCP_PORT=${MAPD_TCP_PORT:=6274}
MAPD_HTTP_PORT=${MAPD_HTTP_PORT:=6278}
MAPD_WEB_PORT=${MAPD_WEB_PORT:=6273}
MAPD_CALCITE_PORT=${MAPD_CALCITE_PORT:=6279}


MAPD_DATA=${MAPD_DATA:="data"}

while (( $# )); do
    case "$1" in
        --read-only)
            RO="--read-only" ;;
        --base-port)
            shift
            MAPD_WEB_PORT=$1
            MAPD_TCP_PORT=$(($MAPD_WEB_PORT-1))
            MAPD_HTTP_PORT=$(($MAPD_WEB_PORT-2))
            MAPD_CALCITE_PORT=$(($MAPD_WEB_PORT+1))
            ;;
        --data)
            shift
            MAPD_DATA=$1
            ;;
        --enable-https)
            HTTPS="--enable-https" ;;
        --cert)
            shift
            HTTPS_CERT="--cert $1" ;;
        --key)
            shift
            HTTPS_KEY="--key $1" ;;
        --config)
            shift
            if [ -e "$1" ]; then
                CONFIG="--config $1"
            else
                echo "WARN: config file does not exist, ignoring: --config $1" >&2
            fi
            ;;
        --non-interactive)
            NON_INTERACTIVE=true ;;
        --disable-open-frontend)
            DISABLE_OPEN_FRONTEND=true ;;
        --verbose)
            VERBOSE="--verbose" ;;
        *)
            break ;;
    esac
    shift
done

echo "Backend TCP:  localhost:${MAPD_TCP_PORT}"
echo "Backend HTTP: localhost:${MAPD_HTTP_PORT}"
echo "Frontend Web: localhost:${MAPD_WEB_PORT}"
echo "Calcite TCP: localhost:${MAPD_CALCITE_PORT}"

if [ ! -d $MAPD_DATA/mapd_data ]; then
    mkdir -p $MAPD_DATA
    ./bin/initdb -f --data $MAPD_DATA

    if [ ! "$NON_INTERACTIVE" = true ]; then
        echo "Download and insert sample data? Y/n"
        read choice
        if [ -z "$choice" ]; then choice="Y"; fi
        case $choice in
            [yY]* )
                INSERT_SAMPLE_DATA=true;;
        esac
    fi
fi

./bin/omnisci_server $MAPD_DATA $RO --port $MAPD_TCP_PORT --http-port $MAPD_HTTP_PORT --calcite-port $MAPD_CALCITE_PORT $CONFIG $VERBOSE $* &
MAPDPID=$!

if [ "$INSERT_SAMPLE_DATA" = true ]; then
    echo "- omnisci_server started"
    echo "- sleeping for 5s while server starts"
    sleep 5

    if [ ! "$NON_INTERACTIVE" = true ]; then
        DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
        . "$DIR/insert_sample_data"
    fi
fi

if [ -d frontend ] && [ -e ./bin/omnisci_web_server ]; then
    ./bin/omnisci_web_server $RO --port $MAPD_WEB_PORT --backend-url "http://localhost:${MAPD_HTTP_PORT}" --data $MAPD_DATA $HTTPS $HTTPS_CERT $HTTPS_KEY $CONFIG $VERBOSE &

    if [ ! -z "$HTTPS" ] ; then
        MAPDFRONTEND="https://localhost:${MAPD_WEB_PORT}"
    else
        MAPDFRONTEND="http://localhost:${MAPD_WEB_PORT}"
    fi

    echo "- sleeping for 5s while server starts"
    sleep 5

    if [ ! "$DISABLE_OPEN_FRONTEND" = true ]; then
        if hash open 2>/dev/null; then
            open "$MAPDFRONTEND" 2> /dev/null || true
        elif hash xdg-open 2>/dev/null; then
            xdg-open "$MAPDFRONTEND" 2> /dev/null || true
        else
            echo "Navigate to: $MAPDFRONTEND"
        fi
    else
        echo "Navigate to: $MAPDFRONTEND"
    fi
fi

wait $MAPDPID
